package hc.j2me.item;

import hc.core.ContextManager;
import hc.core.IConstant;
import hc.core.IScreen;
import hc.core.MsgBuilder;
import hc.core.util.HCURL;
import hc.core.util.HCURLUtil;
import hc.j2me.J2MEURLAction;
import hc.j2me.Starter;
import hc.j2me.util.Jcip;
import hc.j2me.util.OutPortTranser;

import java.io.UnsupportedEncodingException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;

public class Form extends javax.microedition.lcdui.Form implements IPort, IScreen, ItemCommandListener, CommandListener{
	private static Command cmdSubmit = new Command(Starter.getUIString("1025"), Command.SCREEN, 2);
//	private static Command cmdBack = new Command(Starter.getUIString(ScreenUtil.BACK), Command.SCREEN, 1);
	
	public Form() {
		super(Jcip.getInstance().getString(), Jcip.getInstance().getArrItem());
		Jcip.getInstance().init(this);
		
		//搜索是否含有URLAction
		int count = 0;
		int size = super.size();
		//本循环块要注意与下段逻辑同步
		for (int i = 0; i < size; i++) {
			Item item = super.get(i);
			if(item instanceof URLAction){
				int appearanceMode = ((URLAction)item).getAppearanceMode();
				if(appearanceMode == Item.HYPERLINK || appearanceMode == Item.BUTTON){
					count++;
				}
			}
		}
		if(count > 0){
			for (int i = 0; i < size; i++) {
				Item item = super.get(i);
				if(item instanceof URLAction){
					int appearanceMode = ((URLAction)item).getAppearanceMode();
					if(appearanceMode == Item.HYPERLINK || appearanceMode == Item.BUTTON){
						//本循环块要注意与上段逻辑同步

						URLAction urlAction = (URLAction)item;
//						hc.core.L.V=hc.core.L.O?false:LogManager.log("Add Command:" + urlAction.url);
						Command command = new Command(urlAction.cmdTitle, Command.ITEM, 1);
						urlAction.setDefaultCommand(command);
						urlAction.setItemCommandListener(this);
					}
				}
			}
		}
		
		if(ioMode == IConstant.IOMODE_IN_OUT || ioMode == IConstant.IOMODE_ONLY_OUT){
			this.addCommand(cmdSubmit);
		}
		
//		if(ioMode == IConstant.IOMODE_ONLY_IN || ioMode == IConstant.IOMODE_IN_OUT){
//			this.addCommand(cmdRefresh);
//		}
		
		this.addCommand(Starter.BACK_COMMAND);//cmdBack
		
		this.setCommandListener(this);
	}
	
	byte[] formIDBS;
	String formID;
	
	public boolean isSameScreenID(final byte[] bs, final int offset,
			final int len){
		boolean isSame = true;
		if(len == formIDBS.length){
			for (int i = 0, j = offset; j < len; ) {
				if(formIDBS[i++] != bs[j++]){
					isSame = false;
					break;
				}
			}
		}else{
			isSame = false;
		}
		return isSame;
	}

	public String getScreenID(){
		return formID;
	}
	
	public void setScreenID(String screenID) {
		formID = screenID;
		
		try {
			formIDBS = screenID.getBytes(IConstant.UTF_8);;
		} catch (UnsupportedEncodingException e) {
			formIDBS = screenID.getBytes();;
		}
	}
	
	public void setID(String screenID){
		setScreenID(screenID);
	}
	
	public String getID(){
		return getScreenID();
	}
	
	public void update(Jcip jcip){
		int size = size();
		for (int i = 0; i < size; i++) {
			Object obj = get(i);
			if(obj instanceof IPort){
				IPort p = (IPort)obj;
				short iomode = p.getIOMode();
				if(iomode == IConstant.IOMODE_IN_OUT || iomode == IConstant.IOMODE_ONLY_IN){
//					hc.core.L.V=hc.core.L.O?false:LogManager.log("Update IPort");
					p.appendInPort(jcip);
				}
			}
		}
	}
	
//	private static J2MEContext contextInstance = (J2MEContext)ContextManager.getContextInstance();

	public void submit(){
		OutPortTranser opt = new OutPortTranser();
		
		opt.out(formID);
		
		int size = size();
		for (int i = 0; i < size; i++) {
			Object obj = get(i);
			if(obj instanceof IPort){
				IPort p = (IPort)obj;
				short iomode = p.getIOMode();
				if(iomode == IConstant.IOMODE_IN_OUT || iomode == IConstant.IOMODE_ONLY_OUT){
					p.appendOutPort(opt);
				}
			}
		}
		
		ContextManager.getContextInstance().send(MsgBuilder.E_JCIP_FORM_SUBMIT, opt.getSubmitString());					
	}

	short ioMode = 0;
	
	public void setIOMode(short mode) {
		this.ioMode = mode;
	}
	
	public short getIOMode(){
		return ioMode;
	}

	public void appendInPort(Jcip jcip) {
		update(jcip);
	}

	/**
	 * Form是一个特殊处理，本方法无内容
	 */
	public void appendOutPort(OutPortTranser out) {
	}

	public void commandAction(Command c, Item item) {
		String url = ((URLAction)item).url;
		
		HCURLUtil.process(url, J2MEURLAction.defaultJ2MEURLActon);
	}

	public void commandAction(Command c, Displayable disp) {
		if(c == cmdSubmit){
			submit();
			
			//由URL_CMD_EXIT处理退出逻辑
//			return;
		}else if(c == Starter.BACK_COMMAND){
		}
		if(formID != null && formID.length() > 0){
			//关闭E_JCIP_FORM_EXIT，由URL_CMD_EXIT处理退出
//			ContextManager.getContextInstance().send(
//				MsgBuilder.E_JCIP_FORM_EXIT, formID);
			//TODO 支持不同host
			//TODO 支持同formID被多次指引，且可能被嵌入开启
		}
		HCURLUtil.process(HCURL.URL_CMD_EXIT, J2MEURLAction.defaultJ2MEURLActon);
	}

}
